package com.experienceBaby.Cashbook.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.experienceBaby.Cashbook.R;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class MyExpandableAdapter extends BaseExpandableListAdapter {
    List<String> Group;
    List<List<String>> Children;
    Context context;
    final LayoutInflater mInflater;

    public MyExpandableAdapter(Context context, List<String> Group, List<List<String>> Children) {
        this.context = context;
        this.Group = Group;
        this.Children = Children;
        mInflater = LayoutInflater.from(context);
    }


    @Override
    public int getGroupCount() {
        return Group.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return Children.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return Group.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return Children.get(groupPosition).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return groupPosition + childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.listview_main_1, parent, false);
        }
        String group = Group.get(groupPosition);
        String[] strings = group.split(" ");
        TextView tvGroup = convertView.findViewById(R.id.lm_tv1_1);
        TextView tvGroup2 = convertView.findViewById(R.id.lm_tv1_2);
        TextView tvGroup3 = convertView.findViewById(R.id.lm_tv1_3);
        TextView tvGroup4 = convertView.findViewById(R.id.lm_tv1_4);
        tvGroup.setText(strings[0]);
        tvGroup2.setText(strings[1]);
        double d = Double.parseDouble(strings[2]);
        if (d >= 0) {
            tvGroup4.setText("支出：");
        } else {
            tvGroup4.setText("收入：");
        }
        tvGroup3.setText(String.format("%.2f", Math.abs(d)));
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        final String child = Children.get(groupPosition).get(childPosition);
        String[] strings = child.split(" ");
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.listview_main_2, parent, false);
        }
        CircleImageView ivChild = convertView.findViewById(R.id.lm_iv1);

        TextView tvChild = convertView.findViewById(R.id.lm_tv2_1);
        TextView tvChild2 = convertView.findViewById(R.id.lm_tv2_2);
//        tvChild.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Toast.makeText(context, child, Toast.LENGTH_SHORT).show();
//            }
//        });
        //TODO ivChild根据strings[0]设置图片
        int img = 0;
        switch (strings[0]) {
            case "餐饮":
                img = R.drawable.canyin;
                break;
            case "购物":
                img = R.drawable.gouwu;
                break;
            case "日用":
                img = R.drawable.riyong;
                break;
            case "娱乐":
                img = R.drawable.yule;
                break;
            case "服饰":
                img = R.drawable.fushi;
                break;
            //收入
            case "生活费":
                img = R.drawable.shenghuofei;
                break;
            case "工资":
                img = R.drawable.gongzi;
                break;
            case "兼职":
                img = R.drawable.jianzhi;
                break;
            case "红包":
                img = R.drawable.hongbao;
                break;
            default:
                img = R.drawable.hongbao;
                break;
        }
        Glide
                .with(context)
                .load(img)
                .into(ivChild);
        tvChild.setText(strings[1]);
        double d = Double.parseDouble(strings[2]);
        tvChild2.setText(String.format("%.2f", d));
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }



}
